<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function formatTime(time) {
      let date = new Date(time)
      let [year, month, day, hour, minute, second] = [
        date.getFullYear(),
        date.getMonth() + 1,
        date.getDate(),
        date.getHours(),
        date.getMinutes(),
        date.getSeconds()
      ]
      if (month < 10) month = '0' + month
      if (day < 10) day = '0' + day
      if (hour < 10) hour = '0' + hour
      if (minute < 10) minute = '0' + minute
      if (second < 10) second = '0' + second
      let value =
        year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
      return value
    }
    // 日期加30天、60天、180天
    function changeDate(d, n) {
      let addDay = n
      let date = new Date(d)
      date.setDate(date.getDate() + addDay);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      date = year + '-' + month + '-' + day
      console.log('date', date)
      console.log('formatTime', formatTime(date))

    }
    changeDate('2023-08-01', 30)
    changeDate('2023-08-01', 60)
    changeDate('2023-08-01', 180)
    console.log('----------------')
    // 日期加30天、60天、180天
    function changeDate1(d, n) {
      let addDay = Number(n) || 0
      let oldDate = new Date(d)
      let date = new Date(oldDate.getTime() + n * 24 * 3600 * 1000)
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      date = year + '-' + month + '-' + day
      console.log('date', date)
      console.log('formatTime', formatTime(date))
    }
    changeDate1('2023-08-01', 30)
    changeDate1('2023-08-01', 60)
    changeDate1('2023-08-01', 180)


  </script>

</body>

</html>